//
//  MarkplaceViewController.m
//  No15.3
//
//  Created by Bc_Ltf on 15/1/14.
//  Copyright (c) 2015年 Bc_Ltf. All rights reserved.
//

#import "MarkplaceViewController.h"

@interface MarkplaceViewController ()

@end

@implementation MarkplaceViewController
@synthesize textQueryTextField;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*--------------------------------实现点击事件------------------------------*/
- (IBAction)geocodeQuery:(id)sender {
    
    // 空字检测
    if(textQueryTextField.text==nil||[textQueryTextField.text length]==0)
    {
        return;
    }
    
    CLGeocoder *geocoder=[CLGeocoder new];
    // 使用地理信息反编码，来获取位置和信息
    [geocoder geocodeAddressString:textQueryTextField.text completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"查询记录数目：%i",(int)[placemarks count]);
        if ([placemarks count]>0) {
            CLPlacemark *placemark=placemarks[0];
            
            CLLocationCoordinate2D coordinate=placemark.location.coordinate;
            NSDictionary *address=placemark.addressDictionary;
            
            // MKPlacemark 是地图相关信息的类
            MKPlacemark *place=[[MKPlacemark alloc]initWithCoordinate:coordinate/*位置*/ addressDictionary:address/*相关的信息*/];
            
            // MKMapItem 类封装了有关地图上点的信息
            MKMapItem *mapItem=[[MKMapItem alloc]initWithPlacemark:place];
            // 调用苹果地图的方法
            [mapItem openInMapsWithLaunchOptions:nil];
            
            [textQueryTextField resignFirstResponder];
        }
    }];
    
}
@end
